-- SkyRobotM
-- created by panyl
-- 天空之城机器人

module("SkyRobotM", package.seeall);

local robots = {};

-- 载入配置表
local _init = false;
local loadCsv = function()
    if _init then return end
    _init = true;

    -- 转换下trap信息表
    local sky_robot_CSV = get_sky_robot_CSV();
    for i = 1, #sky_robot_CSV do
        robots[sky_robot_CSV[i].id] = sky_robot_CSV[i];
    end
    sky_robot_CSV = {};
end

-- 模块初始化操作
function init()
    loadCsv();
    EventMgr.removeAll("SkyRobotM");
end

function query(id, path)
    local m = robots[id];

    if not m or not path then
        return m;
    end

    return m[path];
end

-- 获取所有的机器人信息
function getAllRobots()
    return ME.user.dbase:query("sky_robot", {});
end

-- 获取机器人数量
function getAmount(id)
    return getAllRobots()[id] or 0;
end

-- 获取某个机器人信息
function getRobotInfo(id)
    local total = getAllRobots()[id] or 0;
    local campRobots = ME.user.dbase:query("camp_robots", {});
    local fighting = 0;

    for _, data in pairs(campRobots) do
        fighting = fighting + (data[id] or 0);
    end

    return {
        ["total"]    = total,
        ["fighting"] = fighting,
    };
end

-- 获取所有出战的机器人
function getRobotsByCamp(camp)
    local campRobots = ME.user.dbase:query("camp_robots", {});
    return campRobots[camp] or {};
end

-- 获取出战机器人属性加成
function getRobotAddon(camp)
    local fightingRobots = getRobotsByCamp(camp);
    local ret = {};

    for id, count in pairs(fightingRobots) do
        local addon = query(id, "addon_prop") or {};

        for attrib, value in pairs(addon) do
            ret[attrib] = (ret[attrib] or 0) + addon[attrib] * count;
        end

        -- 战力属性是另外配置的，所以需要单独计算
        ret["fight_ability"] = query(id, "fight_ability") * count + (ret["fight_ability"] or 0);
    end

    return ret;
end

-- 获取机器人列表的属性加成
function getRobotListAddon(robotList)
    local ret = {};

    for id, count in pairs(robotList) do
        local addon = query(id, "addon_prop") or {};

        for attrib, value in pairs(addon) do
            ret[attrib] = (ret[attrib] or 0) + addon[attrib] * count;
        end

        -- 战力属性是另外配置的，所以需要单独计算
        ret["fight_ability"] = query(id, "fight_ability") * count + (ret["fight_ability"] or 0);
    end

    return ret;
end


-- 增加机器人
function addRobot(id, amount)
    local myRobot = ME.user.dbase:query("sky_robot", {});

    myRobot[id] = (myRobot[id] or 0) + amount;

    ME.user.dbase:set("sky_robot", myRobot);
end

-- 获取可以出战列表
function fetchRobotList()
    local allRobots = getAllRobots();
    local ret = {};

    for id, _ in pairs(allRobots) do
        local robotInfo = getRobotInfo(id);

        if robotInfo["total"] > robotInfo["fighting"] then
            table.insert(ret, id);
        end
    end

    return ret;
end

-- 出战机器人，camp是阵营
function saveRobotGroup(camp, robots)
    -- 当前军队人数
    local armyNum = #SkyCombatM.getGroup(camp);

    for _, amount in pairs(robots) do
        armyNum = armyNum + amount;
    end

    if armyNum > SkyCombatM.getMaxPetNum() then
        return false;
    end

    local fightingRobots = getRobotsByCamp(camp);
    for id, amount in pairs(robots) do
        -- 获取机器人信息
        local robotInfo = getRobotInfo(id);
        local curAmount = fightingRobots[id] or 0;
        if amount + robotInfo["fighting"] - curAmount > robotInfo["total"] then
            -- 超出数量了
            return false;
        end
    end

    -- 记录下来
    local campRobots = ME.user.dbase:query("camp_robots", {});
    campRobots[camp] = robots;
    ME.user.dbase:set("camp_robots", campRobots);

    return true;
end

-- 能否拆解机器人
function canBeSplitRobot(id)
    local split = query(id, "split");
    return type(split) == "table" and #split > 0;
end

-- 拆解一个机器人
function splitRobot(id, amount)
    if not canBeSplitRobot(id) then
        return "cannot_be_split";
    end

    -- 获取机器人信息
    local robotInfo = getRobotInfo(id);
    if not robotInfo then
        return "not_find_robot";
    end

    if robotInfo["fighting"] + amount > robotInfo["total"] then
        return "robot_is_activied";
    end

    -- 先修改数量
    local skyRobot = ME.user.dbase:query("sky_robot", {});
    skyRobot[id] = skyRobot[id] - amount;

    local split = query(id, "split");
    for _, bonus in pairs(split) do
        BonusM.doBonus(bonus, "split_robot");
    end

    -- 发给服务器
    Operation.cmd_split_robot(id, amount);

    return true;
end
